package com.hhu.softpractise.controller;


import com.hhu.softpractise.common.BaseResponse;
import com.hhu.softpractise.common.ErrorCode;
import com.hhu.softpractise.common.ResultUtils;
import com.hhu.softpractise.exception.ThrowUtils;
import com.hhu.softpractise.model.dto.postcomment.PostCommentAddRequest;
import com.hhu.softpractise.model.dto.postcomment.PostCommentEditRequest;
import com.hhu.softpractise.model.dto.postcomment.PostCommentQueryRequest;
import com.hhu.softpractise.model.entiy.PostComment;
import com.hhu.softpractise.model.entiy.User;
import com.hhu.softpractise.service.PostCommentService;
import com.hhu.softpractise.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.util.List;

/**
 * 帖子评论接口
 */
@RestController
@RequestMapping("/post_comment")
@Slf4j
@Api( tags = "帖子评论接口")
public class PostCommentController {

    @Resource
    private UserService userService;
    @Resource
    private PostCommentService postCommentService;

    @ApiOperation( value = "创建帖子评论")
    @PostMapping("/add")
    public BaseResponse<Long> addPostComment(@RequestBody PostCommentAddRequest postCommentAddRequest, HttpServletRequest request){
        ThrowUtils.throwIf(postCommentAddRequest == null, ErrorCode.PARAMS_ERROR);
        Long commentId = postCommentService.addPostComment(postCommentAddRequest, request);
        ThrowUtils.throwIf(commentId <=0 , ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(commentId);
    }

    @ApiOperation( value = "删除帖子评论")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deletePostComment(@RequestBody Long id, HttpServletRequest request){
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        PostComment postComment = postCommentService.getById(id);
        ThrowUtils.throwIf(postComment == null, ErrorCode.NOT_FOUND_ERROR, "帖子评论不存在");
        User loginUser = userService.getLoginUser(request);
        //判断是否为管理员或帖子创建者
        boolean isAdmin = userService.isAdmin(loginUser);
        if(isAdmin){
            return ResultUtils.success(postCommentService.removeById(id));
        }else{
            if(loginUser.getId().equals(postComment.getUserId())){
                return ResultUtils.success(postCommentService.removeById(id));
            }else{
                return ResultUtils.error(ErrorCode.NO_AUTH_ERROR);
            }
        }
    }


    @ApiOperation( value = "编辑帖子评论")
    @PostMapping("/edit")
    public BaseResponse<Boolean> editPostComment(@RequestBody PostCommentEditRequest postCommentEditRequest,
                                                 HttpServletRequest request){
        ThrowUtils.throwIf(postCommentEditRequest == null, ErrorCode.PARAMS_ERROR);
        PostComment postComment = postCommentService.getById(postCommentEditRequest.getId());
        ThrowUtils.throwIf(postComment == null, ErrorCode.NOT_FOUND_ERROR, "帖子评论不存在");
        User loginUser = userService.getLoginUser(request);
        boolean isAdmin = userService.isAdmin(loginUser);
        postComment.setComment(postCommentEditRequest.getComment());
        if(isAdmin){
            return ResultUtils.success(postCommentService.saveOrUpdate(postComment));
        }else{
            if(loginUser.getId().equals(postComment.getUserId())){
                return ResultUtils.success(postCommentService.saveOrUpdate(postComment));
            }else{
                return ResultUtils.error(ErrorCode.NO_AUTH_ERROR);
            }
        }
    }

    @ApiOperation( value = "获取帖子评论")
    @PostMapping("/get")
    public BaseResponse<PostComment> getPostComment(Long id){
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        PostComment postComment = postCommentService.getById(id);
        ThrowUtils.throwIf(postComment == null, ErrorCode.NOT_FOUND_ERROR, "帖子评论不存在");
        return ResultUtils.success(postComment);
    }

    /**
     * 获取帖子评论列表
     * @param postCommentQueryRequest
     * @return
     */
    @ApiOperation( value = "获取帖子评论列表")
    @PostMapping("/list")
    public BaseResponse<List<PostComment>> listPostComment(@RequestBody PostCommentQueryRequest postCommentQueryRequest){
        ThrowUtils.throwIf(postCommentQueryRequest == null, ErrorCode.PARAMS_ERROR);
        List<PostComment> comments = postCommentService.getlist(postCommentQueryRequest);
        return ResultUtils.success(comments);
    }
}
